package org.emergent.lucidity.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.File;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.Writer;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collection;
import java.net.URL;
import java.net.URISyntaxException;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.emergent.lucidity.lucifer.client.LuciferImpl;

/**
 * User: pwoodworth
 * Date: Mar 17, 2008
 * Time: 9:29:15 PM
 */
public class MultiTool
{
    public static String loadStringFromFile( File file )
    {
        StringBuffer result = new StringBuffer();
        InputStream reader = null;
        int inbyte = -1;
        try
        {
            reader = new FileInputStream( file );
            while ((inbyte = reader.read()) != -1)
            {
                result.append( (char)inbyte );
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
            System.err.println( "last byte: " + inbyte );
        }
        finally
        {
            close( reader );
        }
        return result.toString();
    }

    public static File loadSampleAsFile( String filename ) throws FileNotFoundException
    {
        return new File( TestingContext.getSamplesDir(), filename );
    }

    public static InputStream loadSampleAsStream( String filename ) throws FileNotFoundException
    {
        return new FileInputStream( new File( TestingContext.getSamplesDir(), filename ) );
    }

    public static String loadSampleAsString( String filename )
    {
        return MultiTool.loadStringFromFile( new File( TestingContext.getSamplesDir(), filename ) );
    }

    private static Reader getReader( String scriptName )
        throws IOException
    {
        String fqname = "/chrome/content/modules/" + scriptName;
        return new InputStreamReader( OpenPGPExcerciser.class.getResourceAsStream( fqname ) );
    }

    public static String[] loadLines( File file )
        throws IOException
    {
        BufferedReader reader = null;
        Collection retval = new ArrayList();
        try
        {
            reader = new BufferedReader( new FileReader( file ) );
            String line = null;
            while ((line = reader.readLine()) != null)
            {
                if (line.trim().length() > 0)
                    retval.add(line);
            }
        }
        finally
        {
            if (reader != null) try { reader.close(); } catch (Exception ex) {}
        }
        return (String[])retval.toArray( new String[retval.size()] );
    }

    public static void loadModuleScript( Context cx, Scriptable scope, String filename )
        throws IOException
    {
        File dir = TestingContext.getModulesDir();
        Reader reader = null;
        try
        {
            reader = new FileReader( new File( dir, filename ) );
            cx.evaluateReader( scope, reader, filename, 1, null );
        }
        finally
        {
            close( reader );
        }
    }

    private static void relayStreams( InputStream is, OutputStream os ) throws IOException
    {
        int thebyte = -1;
        while ((thebyte = is.read()) > -1)
        {
            os.write( thebyte );
        }
    }

    private static void dumpToFile( File outtemp, String text ) throws IOException
    {
        InputStream is = null;
        OutputStream os = null;
        try
        {
            os = new FileOutputStream( outtemp );
            os.write( LuciferImpl.string2bytes( text ));
            os.flush();
        }
        finally
        {
            close( is );
            close( os );
        }
    }

    private static void close( Reader closeable )
    {
        try { closeable.close(); } catch (Exception e) { }
    }

    private static void close( Writer closeable )
    {
        try { closeable.close(); } catch (Exception e) { }
    }

    private static void close( InputStream closeable )
    {
        try { closeable.close(); } catch (Exception e) { }
    }

    private static void close( OutputStream closeable )
    {
        try { closeable.close(); } catch (Exception e) { }
    }

    public static File saveAsTempSample( String sample ) throws IOException
    {
        File retval = File.createTempFile( "tmp", ".asc", TestingContext.getSamplesDir() );
        retval.deleteOnExit();
        MultiTool.dumpToFile( retval, sample );
        return retval;
    }
}
